<?php
/**
 * Simple message wrapper for logging
 *
 * @package event
 * @subpackage log
 * @author 	   Gerrit Jan van Ahee <gerrit.jan@yes2web.nl>
 * @copyright  2010-2011 Yes2web B.V.
 * @license    MIT License http://www.opensource.org/licenses/mit-license.php
 * @version    1.0.0
 * @link 	   www.yes2web.nl
 */
/**
 * The Event_Log_Message allows for easy message wrapping.
 * 
 * @package event
 * @subpackage log
 * @author 	   Gerrit Jan van Ahee <gerrit.jan@yes2web.nl>
 * @copyright  2010-2011 Yes2web B.V.
 * @license    MIT License http://www.opensource.org/licenses/mit-license.php
 * @version    1.0.0
 * @link 	   www.yes2web.nl
 */
class Event_Log_Message {
	/**
	 * @var String format of this type of message. Used as format string by __toString
	 */
	const FORMAT = '[%s] %s';
	/**
	 * Array of all arguments passed to this instance upon construction.
	 * @var Mixed[]
	 */
	protected $asValue;
	
	/**
	 * Construct a new instance. Throw in as many arguments as you like, just make sure that the format
	 * defined in the message constant has at least as many placeholders. 
	 */
	public function __construct(){
		$this->asValue = func_get_args();
	}
	
	/**
	 * Returns a string containing useful debugging information, e.g. date and ip if known.
	 * It is prefixed by the {@link __toString()} method, but if extending classes have no
	 * need for this, they may omit it.
	 *
	 * @return Sting
	 */
	protected function prefix(){
		return
			date('Y-m-d H:i:s - '). 													# it happend now
			(isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'unknown IP').' ';	# from this location
	}
	
	/**
	 * Construct the log message based on the arguments to the constructor and the format.
	 *
	 * The {@link __toString()} method combines this output with the {@link prefix()} method.
	 *
	 * @return String
	 */
	protected function getMessage(){
		return vsprintf(constant(get_class($this).'::FORMAT'), $this->asValue)."\n"; 	# log these values
	}
	
	/**
	 * Create a String representation of the message
	 * @return String
	 */
	public function __toString(){
		return $this->prefix().$this->getMessage();
			
	}
}